/* Viper Split# Compiler
 * Copyright (c) 2013 Split# Developers
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 */


/* NOTE: The above license DOES NOT APPLY to the Viper and GruntXProductions
 * namespace. Viper is released under the GNU General Public License v3
 */

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using GruntXProductions;
using Viper;
using Viper.Opcodes;

namespace SplitSharp
{
        public partial class CodeGen
        {
                private int getSize (string type)
                {
                        if (type == "string")
                                return 4;
                        if (type == "ptr" || type == "int")
                                return 4;
                        if (type == "char" || type == "byte")
                                return 1;
                        if (type == "short")
                                return 2;
                        return 0;

                }

                private void declareDwordArray (string name, uint size)
                {
                        Assembler.CreateBuffer ("array_" + methodName + "_" + name, size);
                        Variable v = new Variable ();
                        v.Name = name;
                        v.Type = VType.DArray;
                        v.RealName = "array_" + methodName + "_" + name;
                        scope.Peek ().Variables.Add (v);
                }

                private void declareStringArray (string name, uint size)
                {
                        Assembler.CreateBuffer ("array_" + methodName + "_" + name, size);
                        Variable v = new Variable ();
                        v.Name = name;
                        v.Type = VType.StringArray;
                        v.RealName = "array_" + methodName + "_" + name;
                        scope.Peek ().Variables.Add (v);
                }

                private void declareDword (string name)
                {
                        Assembler.CreateBuffer ("int32_" + methodName + "_" + name, 4);
                        Variable v = new Variable ();
                        v.Name = name;
                        v.Type = VType.Dword;
                        v.RealName = "int32_" + methodName + "_" + name;
                        scope.Peek ().Variables.Add (v);
                }

                private string declareTempDword (string name)
                {
                        tmpIndex++;
                        Assembler.CreateBuffer ("int32_" + methodName + "_tmp_" + name + "_index_" + tmpIndex.ToString (), 4);
                        return "int32_" + methodName + "_tmp_" + name + "_index_" + tmpIndex.ToString ();
                }

                private void declareCharPtr (string name)
                {
                        Assembler.CreateBuffer ("charPtr_" + methodName + "_" + name, 4);
                        Variable v = new Variable ();
                        v.Name = name;
                        v.Type = VType.String;
                        v.RealName = "charPtr_" + methodName + "_" + name;
                        scope.Peek ().Variables.Add (v);
                }

                private string declareStringLiteral (string val)
                {
                        stringIndex++;
                        Assembler.CreateString ("stringLiteral_" + methodName + "_index_" + stringIndex.ToString (), val);
                        return "stringLiteral_" + methodName + "_index_" + stringIndex.ToString ();
                }

                private void declareWord (string name)
                {
                        Assembler.CreateBuffer ("int16_" + methodName + "_" + name, 2);
                        Variable v = new Variable ();
                        v.Name = "int16_" + methodName + "_" + name;
                        v.Type = VType.Word;
                        vars.Add (v);
                }

                private void declareChar (string name)
                {
                        Assembler.CreateBuffer ("uint8_" + methodName + "_" + name, 1);
                        Variable v = new Variable ();
                        v.Name = "uint8_" + methodName + "_" + name;
                        v.Type = VType.Byte;
                        vars.Add (v);
                }

                public bool isVariable (string Var)
                {
                        foreach (Scope sp in scope) {
                                foreach (Variable v in sp.Variables) {
                                        if (v.Name == Var)
                                                return true;
                                }
                        }
                        return false;
                }

                public string getVarName (string Var)
                {
                        foreach (Scope sp in scope) {
                                foreach (Variable v in sp.Variables) {
                                        if (v.Name == Var)
                                                return v.RealName;
                                }
                        }
                        return "";
                }

                public VType getVarType (string Var)
                {
                        foreach (Scope sp in scope) {
                                foreach (Variable v in sp.Variables) {
                                        if (v.Name == Var)
                                                return v.Type;
                                }
                        }
                        return VType.Dword;
                }

                public VType getType (List<Token> dat)
                {
                        foreach (Token t in dat) {
                                if (t is Tokens.IntLiteral) {
                                        return VType.Dword;
                                } else if (t is AddressOf) {
                                        return VType.Dword;
                                 } else if (t is PointerDereference) {
                                        return VType.Dword;
                                } else if (t is Tokens.StringLiteral) {
                                        return VType.String;
                                } else if (t is Tokens.Statement) {
                                        return getVarType (t.ToString ());
                                }
                        }
                        return VType.Dword;
                }

                public bool isFunction (string name)
                {
                        foreach (CodeElement cl in Ast) {
                                if (cl is Function) {
                                        Function func = cl as Function;
                                        if (func.Name == name)
                                                return true;
                                }
                        }
                        return false;
                }

                public Function getFunction (string name)
                {
                        foreach (CodeElement cl in Ast) {
                                if (cl is Function) {
                                        Function func = cl as Function;
                                        if (func.Name == name)
                                                return func;
                                }
                        }

                        return null;
                }
        }
}

